package cn.com.taiji.oospub.wechat.service.impl;

import cn.com.taiji.oospub.wechat.entity.CoursesOrders;
import cn.com.taiji.oospub.wechat.mapper.CoursesOrdersMapper;
import cn.com.taiji.oospub.wechat.service.ICoursesOrdersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.dreamyoung.mprelation.AutoMapper;
import com.github.dreamyoung.mprelation.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yzd
 * @since 2020-10-28
 */
@Service
public class CoursesOrdersServiceImpl extends ServiceImpl<CoursesOrdersMapper, CoursesOrders> implements ICoursesOrdersService {

    @Autowired
    private CoursesOrdersMapper coursesOrdersMapper;
    @Autowired
    private AutoMapper autoMapper;

    @Override
    public List<CoursesOrders> listByTime(Calendar calendar, Calendar calendar1, Calendar calendar2, Calendar calendar3) {
        QueryWrapper<CoursesOrders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", false);

        Calendar clone = (Calendar) calendar.clone();
        calendar.add(Calendar.HOUR_OF_DAY, 1);
        clone.add(Calendar.HOUR_OF_DAY, -1);
        queryWrapper.gt("pay_time", clone);
        queryWrapper.lt("pay_time", calendar);

        Calendar clone1 = (Calendar) calendar.clone();
        calendar1.add(Calendar.HOUR_OF_DAY, 1);
        clone.add(Calendar.HOUR_OF_DAY, -1);
        queryWrapper.or(i -> i.gt("pay_time", clone1).lt("pay_time", calendar1));

        Calendar clone2 = (Calendar) calendar.clone();
        calendar2.add(Calendar.HOUR_OF_DAY, 1);
        clone.add(Calendar.HOUR_OF_DAY, -1);
        queryWrapper.or(i -> i.gt("pay_time", clone2).lt("pay_time", calendar2));


        Calendar clone3 = (Calendar) calendar.clone();
        calendar3.add(Calendar.HOUR_OF_DAY, 1);
        clone.add(Calendar.HOUR_OF_DAY, -1);
        queryWrapper.or(i -> i.gt("pay_time", clone3).lt("pay_time", calendar3));

        return coursesOrdersMapper.selectList(queryWrapper);
    }

    @Override
    public CoursesOrders findById(String id) {
        return coursesOrdersMapper.selectById(id);
    }

    @Override
    public CoursesOrders findByProperty(String columnName, String value) {
        QueryWrapper<CoursesOrders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(columnName, value);
        return coursesOrdersMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean updateOrderStatusByMealCode(String mealCode, CoursesOrders.CoursesOrderStatus oldStatus, CoursesOrders.CoursesOrderStatus newStatus) {
        UpdateWrapper<CoursesOrders> coursesOrdersUpdateWrapper = new UpdateWrapper<>();
        coursesOrdersUpdateWrapper.eq("status", oldStatus);
        coursesOrdersUpdateWrapper.eq("meal_code", mealCode);
        coursesOrdersUpdateWrapper.ge("take_time", LocalDate.now().plusDays(1));
        coursesOrdersUpdateWrapper.le("take_time", LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.MAX));
        coursesOrdersUpdateWrapper.set("status", newStatus);
        return this.update(coursesOrdersUpdateWrapper);
    }

    @Override
    public boolean updateOrderStatus(String mealCode, LocalDate localDate, CoursesOrders.CoursesOrderStatus oldStatus, CoursesOrders.CoursesOrderStatus newStatus) {
        UpdateWrapper<CoursesOrders> coursesOrdersUpdateWrapper = new UpdateWrapper<>();
        coursesOrdersUpdateWrapper.eq("status", oldStatus);
        if (StringUtils.hasText(mealCode)) {
            coursesOrdersUpdateWrapper.eq("meal_code", mealCode);
        }
        coursesOrdersUpdateWrapper.ge("take_time", localDate);
        coursesOrdersUpdateWrapper.le("take_time", LocalDateTime.of(localDate, LocalTime.MAX));
        coursesOrdersUpdateWrapper.set("status", newStatus);
        return this.update(coursesOrdersUpdateWrapper);
    }

    @Override
    public void updateStatus(CoursesOrders orders) {

        coursesOrdersMapper.updateById(orders);
    }

    @Override
    public List<CoursesOrders> queryList(QueryWrapper<CoursesOrders> queryModel) {
        List<CoursesOrders> coursesOrders = coursesOrdersMapper.selectList(queryModel);
        for (CoursesOrders coursesOrder : coursesOrders) {
            autoMapper.mapperEntity(coursesOrder);
        }
        return coursesOrders;
    }

    @Override
    public Page queryPage(Page<CoursesOrders> page, QueryWrapper<CoursesOrders> queryModel) {
        page.addOrder(OrderItem.desc("create_time"));
        Page<CoursesOrders> coursesOrdersPage = coursesOrdersMapper.selectPage(page, queryModel);
        for (CoursesOrders coursesOrders : coursesOrdersPage.getRecords()) {
            autoMapper.mapperEntity(coursesOrders);
        }
        return coursesOrdersPage;
    }
}
